package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.CouponListBean;
import com.dmooo.yxmmerchant.view.contract.CouponContract;
import com.dmooo.yxmmerchant.view.model.CouponModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

public class CouponPresenter extends BasePresenter<CouponContract.CouponView> implements CouponContract.CouponPtr {
    private CouponModel model;
    private GsonBuilder builder=new GsonBuilder();

    public CouponPresenter(CouponContract.CouponView mView, Context context) {
        super(mView);
        model=new CouponModel(context);
        builder.serializeNulls();
    }

    @Override
    public void addCoupon(String token, String name, String money, String condition_money, String all_num,String limit_num, String send_start_time, String send_end_time, String use_start_time, String use_end_time, String type, String goods_id) {
        model.addCoupon(token, name, money, condition_money, all_num, limit_num,send_start_time, send_end_time, use_start_time, use_end_time, type, goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addCouponSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getCouponList(String token, int page) {
        model.getCouponList(token, page, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    JSONObject obj=new JSONObject(builder.create().toJson(o));
                    mView.showCouponList(new Gson().fromJson(obj.toString(),CouponListBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void delCoupon(String token, String coupon_id, int pos) {
        model.delCoupon(token, coupon_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.delCouponSuccess(pos);
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editCoupon(String token, String coupon_id, String name, String money, String condition_money, String all_num, String limit_num, String send_start_time, String send_end_time, String use_start_time, String use_end_time, String type, String goods_id) {
        model.editCoupon(token,coupon_id, name, money, condition_money, all_num, limit_num,send_start_time, send_end_time, use_start_time, use_end_time, type, goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editCouponSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
